//
//  CarInfoCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/12/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


final class ItemsStack: UIStackView {
    
    struct KeyValueItem {
        var value: String
        var hint: String
    }
    
    init () {
        super.init(frame: .zero)
        self.setUpSelf()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func setUpItems (data: [KeyValueItem]) {
        _ = arrangedSubviews.map {
            removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        let hints = data.map {
            $0.hint
        }
        let values = data.map {
            $0.value
        }
        self.addRows(data: hints, isValue: false)
        self.addRows(data: values, isValue: true)
    }
    
    private func setUpSelf () {
        axis = .horizontal
        alignment = .leading
        distribution = .fillProportionally
        spacing = 30
    }
    
    private func addRows (data: [String], isValue: Bool) {
        let columnStack = UIStackView()
        columnStack.axis = .vertical
        columnStack.alignment = .top
        columnStack.distribution = .fill
        columnStack.spacing = 16.0
        
        for item in data {
            let itemLbl = UILabel()
            itemLbl.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            itemLbl.textColor = isValue ? .darkBlueColor : .greyColor
            itemLbl.text = item
            itemLbl.sizeToFit()
            columnStack.addArrangedSubview(itemLbl)
        }
        
        addArrangedSubview(columnStack)
    }
}


final class CarInfoCell: UITableViewCell, ConfigurableCell, MainVCCell {

    // MARK: - Outlets

    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        lbl.textAlignment = .left
        lbl.sizeToFit()
        lbl.textColor = .black
        lbl.text = "Характеристики"
        return lbl
    }()
    
    private lazy var infoStackView = ItemsStack()
    
    private lazy var allInfoBtn: MoreButton = {
        let btn = MoreButton()
        btn.setUp(text: "Все характеристики")
        return btn
    }()
    
    private lazy var suiteBtn: MoreButton = {
        let btn = MoreButton()
        btn.setUp(text: "Комплектации")
        return btn
    }()
    
    private lazy var separatorView = CellSeparator()
    
    // MARK: - Props

    private var data: MainViewModel.CarInfoItems!
    var delegate: MainVCCellActionsDelegate!
    
    // MARK: - Actions
    
    @objc private func openKomplektations () {
        delegate.openKomplektations()
    }
    
    @objc private func openFullInfo () {
        delegate.openFullInfo()
    }

    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure(data object: MainViewModel.CarInfoItems?, index: Int) {
        self.data = object!
        infoStackView.setUpItems(data: data.items)
    }

    func returnData() -> MainViewModel.CarInfoItems? {
        return nil
    }
}


extension CarInfoCell {
    
    private func addViews () {
        self.contentView.addSubview(titleLbl)
        self.contentView.addSubview(infoStackView)
        self.contentView.addSubview(allInfoBtn)
        self.contentView.addSubview(suiteBtn)
        self.contentView.addSubview(separatorView)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(openKomplektations))
        suiteBtn.addGestureRecognizer(tap)
        
        let infoTap = UITapGestureRecognizer(target: self, action: #selector(openFullInfo))
        allInfoBtn.addGestureRecognizer(infoTap)
        
        titleLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(22)
            make.height.equalTo(20)
        }
        
        separatorView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.equalToSuperview()
        }
        
        suiteBtn.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(separatorView.snp.top).offset(-20)
        }
        
        allInfoBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(suiteBtn.snp.top).offset(-8)
            make.leading.trailing.equalToSuperview().inset(16)
        }
            
        infoStackView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLbl.snp.bottom).offset(18)
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(allInfoBtn.snp.top).offset(-16)
        }
    }
}
